package com.enba.mallapi.utils;

import com.enba.boot.redis.utils.CodeGeneratorUtil;
import com.enba.mallapi.module.auth.mapper.OrgMapper;
import com.enba.mallapi.module.auth.mapper.PlatformUserMapper;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/** 编码生成工具类 */
@Component
public class CodeGenerator {

  @Autowired private CodeGeneratorUtil codeGeneratorUtil;
  @Autowired private OrgMapper orgMapper;
  @Autowired private PlatformUserMapper userMapper;

  private String generateCode(
      String prefix, int length, String redisKey, Function<Void, String> maxCodeGetter) {
    return codeGeneratorUtil.generateCode(prefix, length, redisKey, maxCodeGetter);
  }

  /**
   * 组织机构编码生成
   *
   * @return 组织机构编码
   */
  public String orgGenerateCode() {
    return generateCode("", 6, "CodeGenerator:org", (e) -> orgMapper.getMaxCode());
  }

  /**
   * 用户编码生成
   *
   * @return 用户编码
   */
  public String userGenerateCode() {
    return generateCode("U", 8, "CodeGenerator:user", (e) -> userMapper.getMaxCode());
  }
}
